const express = require("express");
const app = express();
const mongoose = require('mongoose');
const bodyParser = require("body-parser");
const passport = require("passport");
const compression = require('compression');

const usersRouter=require("./routes/api/users.js");
const workshopRouter=require("./routes/api/workshop.js");
const billetWarehouseRouter =require("./routes/api/billetwarehouse.js");
const billetedWarehouseRouter =require("./routes/api/billetedwarehouse.js");
const processingCompanyRouter =require("./routes/api/processingCompany.js");
const customerRouter = require("./routes/api/customer.js");
const varietyRouter = require("./routes/api/variety.js");
const billRouter = require("./routes/api/bill.js");
const inventoryRouter = require("./routes/api/inventory.js");

app.use(compression());


//连接mongodb数据库
mongoose.Promise=global.Promise;
mongoose.connect('mongodb://localhost/manageSystem', {useNewUrlParser: true,useUnifiedTopology:true})
.then(()=>console.log('connection successful!'))
.catch((err)=>console.error(err));

//加载bodyParser中间件
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());

//初始化passport
app.use(passport.initialize());
require("./config/passport")(passport);

//设置路由
app.use("/api/user",usersRouter);
app.use("/api/workshop",workshopRouter);
app.use('/api/billetWarehouse', billetWarehouseRouter);
app.use('/api/billetedWarehouse', billetedWarehouseRouter);
app.use('/api/processingCompany', processingCompanyRouter);
app.use('/api/customer', customerRouter);
app.use('/api/variety', varietyRouter);
app.use('/api/bill', billRouter);
app.use('/api/inventory', inventoryRouter);
// console.log(process.env.NODE_ENV)
// if(process.env.NODE_ENV === 'production'){
    app.use(express.static(__dirname + '/public/dist/'));
    app.get(/.*/, (req, res)=> res.sendFile(__dirname + '/public/dist/index.html'));
// }

const port = process.env.PORT || 5000;

app.listen(port,()=>{
    console.log(`Server running on port ${port}`);
})


const { exec } = require('child_process');
const path = require('path');
const moment = require('moment')
const fs = require('fs');
const schedule = require('node-schedule');

 

 
// 备份MongoDB数据库的函数
function backupDatabase(databaseUri, backupPath) {
  const mongodumpPath = path.join(__dirname, 'node_modules/.bin/mongodump');
  const command = `${mongodumpPath} --uri=${databaseUri} --out=${backupPath}`;
  const time =moment().format('YYYY-MM-DD')
  const newPath = path.join(backupPath, time)
  fs.mkdir(newPath, (err) => {
    if (err) {
      console.error(err);
    } else {
    //   console.log('文件夹创建成功');
    exec(command, (error, stdout, stderr) => {
        if (error) {
          console.error(`备份失败: ${error.message}`);
          return;
        }
        if (stderr) {
          console.error(`备份时有错误输出: ${stderr}`);
        }
        console.log('备份成功.');
      });
    }
  });
 
  
}
 
// 使用示例



 
// 定义任务函数
function myTask() {
    const databaseUri = 'mongodb://localhost/manageSystem';
    const backupPath = './dbdata';
     
    backupDatabase(databaseUri, backupPath);
}

 
// 每天的下午5点30分执行任务
const job = schedule.scheduleJob('00 08 * * *', myTask);

function removeAll() {
    for (let i in schedule.scheduledJobs) {
      schedule.cancelJob(i);
    }
  }
